<?php
namespace Systemht\Controller;
use Think\Controller;
class PictController extends IndexController{
	public function index(){
		//搜索
		$cate_id = I('get.cate_id');
		if(!empty($cate_id)){
			$where['cate_id'] = $cate_id;
		}
		$this->assign('cate_id',$cate_id);
		//分页
		$where['status'] = 1;
		$model = M('Pict');
		$count = $model->where($where)->count();
		$Page       = new \Think\Page($count,C('PAGE_NUM'));
		$show       = $Page->Homeshow();
		$Page->lastSuffix = false;
		$this->assign('show',$show);
		//分页查询数据
		$data = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('data',$data);
		//获取图片分类信息
		$m_cate = M('pict_cate');
		$pict = $m_cate->select();
		$this->assign('pict',$pict);
		$this->display();
	}
	public function add(){
		if(IS_POST){
			$model = D('Pict');
			if($model->create(I('post.'),1)){
				if($model->add()){
					$this->success('添加成功',U('add'));
					exit;
				}
			}
			$this->error($model->getError());
		}
		//获取图片分类信息
		$m_cate = M('pict_cate');
		$pict = $m_cate->select();
		$this->assign('pict',$pict);
		$this->display();
	}
	public function edit(){
		$model = D('Pict');
		if(IS_POST){
			if($model->create(I('post.'),2)){
				if(false !== $model->save()){
					$this->success('修改成功',U('index'));
					exit;
				}
			}
			$this->error($model->getError());
		}
		$id = I('get.id');
		$data = $model->where(array('id'=>$id))->find();
		$this->assign('data',$data);
		//获取图片分类信息
		$m_cate = M('pict_cate');
		$pict = $m_cate->select();
		$this->assign('pict',$pict);
		$this->display();
	}
	public function del(){
		$id = I('post.id');
		$result = M('Pict')->where(array('id'=>$id))->delete();
		if($result){
			$this->ajaxReturn(array('status'=>"OK",'msg'=>'删除成功'));
		}
		else{
			$this->ajaxReturn(array('status'=>"NO",'msg'=>'删除失败'));
		}
	}
}